﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using EnvironmentManagerSystem.MODE;

namespace EnvironmentManagerSystem.ConstructionUnit
{
    public partial class AddConstructionUnit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                clear();
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                try
                {
                    ConstructionUnitData cud = new ConstructionUnitData();
                    cud.Name = txtjsdw.Text.Trim();
                    cud.Address = txttxdz.Text.Trim();
                    cud.Phone = txtlxdh.Text.Trim();
                    cud.ZipCode = txtyzbm.Text.Trim();
                    cud.chuanzhen = txtlxr0.Text.Trim();
                    cud.youxiang = txtlxr1.Text.Trim();
                    cud.Lianxiren = txtlxr.Text.Trim();
                    cud.Ren = txtfrdb.Text.Trim();
                    cud.uid = uid.Text;
                    cud.pwd = pwd.Text;
                    UserDataCollection lst = EnvironmentManagerSystem.BLL.ConstructionUnit.GetByUserName(txtjsdw.Text.Trim());
                    if (lst != null)
                    {
                        if (EnvironmentManagerSystem.BLL.ConstructionUnit.AddConstructionUnit(cud))
                        {
                            //Response.Write("<script>window.parent.location.href = '../login.aspx';</script>");
                            Response.Write("<script>alert('注册成功');window.parent.frm.dalog.close();</script>");
                        }
                        else
                        {
                            Jscript.Alert("用户名称已存在", this);
                        }
                    }
                    else
                    {
                        foreach (var item in lst)
                        {
                            string LoginName = item.LoginName;
                            string UserName = item.UserName;
                            Jscript.Alert("建设单位已存在!您的登录名称是:" + LoginName + "", this);
                        }
                    }
                }
                catch { }
            }
        }

        //重置
        protected void btnClear_Click(object sender, EventArgs e)
        {
            clear();
        }

        /// <summary>
        /// 清空数据方法
        /// </summary>
        public void clear()
        {
            txtfrdb.Text = "";
            txtjsdw.Text = "";
            txtlxdh.Text = "";
            txtlxr.Text = "";
            txtyzbm.Text = "";
            txttxdz.Text = "";
            txtlxr0.Text = "";
            txtlxr1.Text = "";
            txtlxr2.Text = "";
            uid.Text = "";
            pwd.Text = "";
            uid.Focus();
        }

        /// <summary>
        /// 验证文本框
        /// </summary>
        public Boolean Check()
        {
            if (!ValidateCode1.CheckSN(txtlxr2.Text))
            {
                Jscript.Alert("验证码错误", this);
                return false;
            }
            if (!pwd.Text.Trim().Equals(pwd0.Text.Trim()))
            {
                Jscript.Alert("密码与确认密码不一致", this);
                return false;
            }
            if (!txtfrdb.Text.Trim().Equals("")
                && !txtjsdw.Text.Trim().Equals("")
                && !uid.Text.Trim().Equals("")
                && !pwd.Text.Trim().Equals("")
                && !txtlxdh.Text.Trim().Equals("")
                && !txtyzbm.Text.Trim().Equals("")
                && !txttxdz.Text.Trim().Equals("")
                && !txtlxr0.Text.Trim().Equals("")
                && !txtlxr1.Text.Trim().Equals("")
                && !txtlxr.Text.Trim().Equals(""))
            {
                //if (Regex.IsMatch(txtlxdh.Text.Trim(), @"^((1\d{10})|(1\d{10}[, ，]))+$"))
                if (Regex.IsMatch(txtlxdh.Text.Trim(), @"^[0-9]*$"))
                {
                    return true;
                }
                else
                {
                    Jscript.Alert("联系电话输入错误", this);
                    return false;
                }
            }
            else
            {
                Jscript.Alert("不能有空值", this);
                return false;
            }
        }
    }
}
